<template>
    <j-modal
      width="80%"
      :title="title"
      :maskClosable="false"
      :switchFullscreen="true"
      :destroyOnClose="true"
      :visible.sync="editModalVisible"
      @cancel="handleCancleDbSync"
    >
    <template slot="footer">
      <a-button @click="handleCancleDbSync">
        关闭
      </a-button>
      <a-button type="primary" :loading="syncLoading" @click="handleDbSync">确认</a-button>
    </template>
    <div class="table-page-search-wrapper warp">
      <a-form-model layout="horizontal" labelAlign="right" ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :model="formData" :rules="rules">
        <a-row :gutter="24">
          <a-col :xl="8" :lg="8" :md="8" :sm="24" >
            <a-form-model-item label="文件标题" prop="title">
              <a-input :disabled="disableSubmit" v-model="formData.title" placeholder="请输入文件标题"/>
            </a-form-model-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24" >
            <a-form-model-item label="文件编码" prop="code">
              <a-input v-model="formData.code" :disabled="true" placeholder="请输入文件编码"/>
            </a-form-model-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24">
            <a-form-model-item label="发文日期" prop="outDate">
              <a-date-picker style="width:100%;" :disabled="disableSubmit" v-model="formData.outDate"/>
            </a-form-model-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24">
            <a-form-model-item prop="deptCode" label="公司">
              <!-- <a-select mode="multiple" v-model="formData.deptCode" :disabled="disableSubmit">
                <template v-for="(item,i) in deptList ">
                  <a-select-option :key="i" :value="item.orgCode">{{item.departName}}</a-select-option>
                </template>
              </a-select> -->
              <JMultiSelectTag v-model="formData.deptCode" :disabled="disableSubmit" :options="deptList"  />
            </a-form-model-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24">
            <a-form-model-item label="文件类型" prop="type">
              <j-dict-select-tag
                :disabled="disableSubmit"
                v-model="formData.type"
                placeholder="请选择"
                dict-code="hr_officedoc_type"
              >
              </j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24" >
            <a-form-model-item label="签发人" prop="signer">
              <JSelectUserByDep :disabled="disableSubmit" v-model="formData.signer" />
            </a-form-model-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24" >
            <a-form-model-item label="文件上传">
              <j-upload fileType="pdf" :disabled="disableSubmit" v-model="formData.file" />
            </a-form-model-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24" >
            <a-form-model-item label="附件">
              <j-upload :disabled="disableSubmit" v-model="formData.appendix" />
            </a-form-model-item>
          </a-col>
          <a-col :xl="8" :lg="8" :md="8" :sm="24">
            <a-form-model-item label="置顶">
              <a-radio-group :disabled="disableSubmit" v-model="formData.topType">
                <a-radio value="1">不置顶</a-radio>
                <a-radio value="2">置顶</a-radio>
                <a-radio value="3">永久置顶</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
  </j-modal>
</template>
<script>
import { postAction, putAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import moment from 'moment'
import JUpload from '@/components/jeecg/JUpload'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
import { getSn } from '@/api/api'
import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
export default {
  name: 'OaOfficialdocIssuedModal',
  components: {
    JUpload,
    // JSelectMultiUser,
    JSelectUserByDep,
    JMultiSelectTag
  },
  data() {
    return {
      editModalVisible: false,
      disableSubmit: false,
      title: '新增',
      rules: {
        title: [
          { required: true, message: '请输入文件名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入文件编码', trigger: 'blur' },
          { validator: (rule, value, callback) => validateDuplicateValue('hr_office_doc', 'code', value, this.formData.id, callback) }
        ],
        outDate: [
          { required: true, message: '请输入发文日期', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请选择文件类型', trigger: 'blur' }
        ],
        deptCode: [
          { required: true, message: '请输入公司', trigger: 'blur' }
        ],
        signer: [
          { required: true, message: '请选择签发人', trigger: 'blur' }
        ]
      },
      formData: {},
      syncLoading: false,
      url: {
        add: '/hr/officedoc/add',
        edit: '/hr/officedoc/edit',
        dept: '/hr/officedoc/queryDocOrg'
      },
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      deptList: []
    }
  },
  methods: {
    getDept() {
      getAction(this.url.dept).then(res => {
        if (res.success) {
          this.deptList = []
          res.result.forEach(item => {
            this.deptList.push({
              value:item.orgCode,
              label:item.departName
            })
          })
          //this.deptList = res.result
        }
      })
    },
    handleCancleDbSync() {
      this.disableSubmit = false
      this.$refs['ruleForm'].resetFields()
      this.editModalVisible = false
    },
    add() {
      this.formData = {
        topType: '1',
        status: '2'
      }
      this.editModalVisible = true
    },
    edit(data) {
      this.formData = JSON.parse(JSON.stringify(data))
      this.editModalVisible = true
    },
    async handleDbSync() {
      if (!this.formData.id && !this.formData.code) {
        await this.getCode()
      }
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          this.updateData()
        }
      })
    },
    updateData() {
      this.syncLoading = true
      this.formData.outDate = moment(this.formData.outDate).format('YYYY-MM-DD HH:mm:ss')
      if (this.formData.id) {
        putAction(this.url.edit, this.formData).then(res => {
          this.actionSuccess(res)
        })
      } else {
        postAction(this.url.add, this.formData).then(res => {
          this.actionSuccess(res)
        })
      }
    },
    actionSuccess(res) {
      this.syncLoading = false
      if (res.success) {
        this.$emit('ok')
        this.editModalVisible = false
      } else {
        this.$message.warning(res.message)
      }
    },
    getCode(val) {
      return getSn('XY-' + filterDictTextByCache('hr_officedoc_type', this.formData.type) + '-${YYYY}-${MM}-###', {}).then(res => {
        if (res.success) {
          this.formData.code = res.result
        } else {
          this.$message.warning(res.message)
        }
      })
    }
  },
  mounted() {
    this.getDept()
  }
}
</script>
<style lang="less">

</style>
